package com.happypets.backstage;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.happypets.model.ManagerBean;
import com.happypets.servicesImpl.ManagerServiceImpl;

/**
 * Servlet implementation class ManagerDeal
 */
public class ManagerDeal extends HttpServlet {
	private static final long serialVersionUID = 1L;
	ManagerServiceImpl msi = new ManagerServiceImpl();
    public ManagerDeal() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=UTF-8");
		response.setHeader("Cache-Control", "no-store"); 
		PrintWriter out = response.getWriter();
		
		
		
		String flag = request.getParameter("flag");
		
		System.out.println(flag);
		
		//1 show  0 show only one  
		List<ManagerBean> lm = new ArrayList<ManagerBean>();
		switch(flag){
		case "1" : lm = showAllManager(request); 				break;
		case "2" : lm = showOneManager(request); 			break;
		case "3" : lm = updateManager(request);				break;
		case "4" : lm = deleteManager(request);					break;
		case "5" : lm = addManager(request);					break;
		}
		
		
		Gson gson=new Gson();
		String json=gson.toJson(lm);
		System.out.println("管理员\n"+json);
		out.write(json);
		
		out.flush();
		out.close();
		
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	
	public List<ManagerBean> showAllManager(HttpServletRequest request){
		
		String pageNumber = request.getParameter("pageNumber");
		String showNumber = request.getParameter("showNumber");
		
		return msi.allManagers(pageNumber,showNumber);
		
	}
	
	
	public List<ManagerBean> showOneManager(HttpServletRequest request){
		
		String id = request.getParameter("id");
		
		List<ManagerBean> lm = new ArrayList<ManagerBean>();
		
		lm.add(msi.managerInfo(id));
		
		return lm;
		
	}
	
	public List<ManagerBean> updateManager(HttpServletRequest request){
		
		ManagerBean mb = new ManagerBean();
		
		mb.setId(request.getParameter("id"));
		mb.setName(request.getParameter("userName"));
		mb.setPasswd(request.getParameter("password"));
		String[] ps = request.getParameterValues("power");
		mb.setPower(sToL(ps));
		int f = msi.updateManager(mb);
		String msg = f == 1?"修改成功！":"修改失败！";
		mb.setName(msg);
		
		List<ManagerBean> l = new ArrayList<ManagerBean>();
		
		l.add(mb);
		return l;
	}
	
	public List<ManagerBean> deleteManager(HttpServletRequest request){
		String id = request.getParameter("id");
		int f = msi.deleteManager(id);
		
		String msg = f == 1?"删除成功！":"删除失败！请去除权限后再尝试删除！";
		
		ManagerBean mb = new ManagerBean();
		
		mb.setName(msg);
		
		List<ManagerBean> l = new ArrayList<ManagerBean>();
		
		l.add(mb);
		return l;
	}
	
	public List<ManagerBean> addManager(HttpServletRequest request){
		
		ManagerBean mb = new ManagerBean();
		
		mb.setName(request.getParameter("userName"));
		mb.setPasswd(request.getParameter("password"));
		String[] ps = request.getParameterValues("power");
		mb.setPower(sToL(ps));
		
		int f = msi.addManager(mb);
		String msg = f == 1?"添加成功！":"添加失败！";
		mb.setName(msg);
		
		List<ManagerBean> l = new ArrayList<ManagerBean>();
		
		l.add(mb);
		return l;
	}
	
	/**
	 * 
	 * @param ps
	 * @return
	 */
	private List<String> sToL(String[] ps){
		List<String> ls = new ArrayList<String>();
		
		if(ps != null && ps.length>0 ){
			for(String t:ps){
				ls.add(t);
			}
			return ls;
		}else{
			return null;
		}
		
	}

}
